package com.detection.controller;

import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.utils.HttpUtils;
import com.detection.common.config.DetectionConfig;
import com.detection.dahua.config.OauthConfigUtil;
import com.detection.dahua.demo.video.realTimePreview.RealTimePreviewDemo;
import com.detection.dahua.model.brm.device.DeviceTreeRequest;
import com.detection.dahua.model.datasync.DataSync;
import com.detection.dahua.model.video.realTimePreview.HlsUrlRequest;
import com.detection.dahua.model.video.realTimePreview.HlsUrlResponse;
import com.detection.dahua.service.IDahuaDeviceService;
import com.detection.tcp.client.NettyTcpClient;
import com.detection.tcp.domain.TCPMessage;
import com.detection.tcp.filetransfer.client.FileUploadClient;
import com.example.network.demos.tcp.domain.FileUploadFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;
import java.util.Objects;

@Api(tags = "test")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private NettyTcpClient nettyTcpClient;

    @Autowired
    private IDahuaDeviceService dahuaDeviceService;

    @Autowired
    private RealTimePreviewDemo realTimePreviewDemo;
    @Autowired
    private FileUploadClient fileUploadClient;
    /**
     * 接收YOLO事件
     */
    @ApiOperation("tcp测试")
    @PostMapping("/tcp-push")
    public void receive(TCPMessage message) {
        nettyTcpClient.sendMessage(message);
    }

    @ApiOperation("tcp测试")
    @PostMapping("/receiveAlarmMsg")
    public void receiveAlarmMsg(@RequestBody JSONObject data) {
        System.out.println(data);
        TCPMessage tcpMessage = TCPMessage.builder()
                .flag("subscribeAlarmEvent")
                .data(data)
                .campCode(DetectionConfig.getCampCode())
                .build();
        nettyTcpClient.sendMessage(tcpMessage);
    }

    @ApiOperation("tcp测试fasongwenjian")
    @GetMapping("/sendFile")
    public void sendFile(String fileFullPath) throws Exception {
        FileUploadFile uploadFile = new FileUploadFile();
        File file = new File(fileFullPath);
        String fileMd5 = file.getName();// 文件名
        uploadFile.setFile(file);
        uploadFile.setFile_md5(fileMd5);
        uploadFile.setStarPos(0);// 文件开始位置
        uploadFile.setFilePath(fileFullPath.replace(DetectionConfig.getProfile(),""));
        fileUploadClient.connect(uploadFile);
        System.out.println("发送文件成功: "+fileFullPath);
    }

    /**
     * 测试获取设备树
     *
     * @return
     */
    @ApiOperation("测试同步设备树")
    @PostMapping("/test/getdevicetree")
    public List<DataSync> testGetHlsUrlForTest(@RequestBody DeviceTreeRequest deviceTreeRequest) {
        deviceTreeRequest.setType("001;;1");
        deviceTreeRequest.setId("001001");
        deviceTreeRequest.setCheckStat(1);
        return dahuaDeviceService.getDeviceTreeSync(deviceTreeRequest);
    }
    @ApiOperation("启动视频线程")
    @PostMapping("/getHlsUrl")
    public String startDahuaDevice(String channelId,String streamType,String type) throws ClientException {
        if(Objects.isNull(channelId)){
            throw new RuntimeException("通道ID不能为空！！");
        }
        if(Objects.isNull(streamType)){
            throw new RuntimeException("码流类型不能为空！！");
        }
        if(Objects.isNull(type)){
            throw new RuntimeException("协议类型不能为空！！");
        }
        HlsUrlRequest hlsUrlRequest = new HlsUrlRequest();
        HlsUrlRequest.Data data = new HlsUrlRequest.Data();
        data.setChannelId(channelId);
        data.setStreamType(streamType);
        data.setType(type);
        hlsUrlRequest.setData(data);
        HlsUrlResponse hlsUrlResponse = realTimePreviewDemo.getHlsUrl(hlsUrlRequest);
        String hlsPath = hlsUrlResponse.getData().getUrl();
        String token = HttpUtils.getToken(OauthConfigUtil.getOauthConfig()).getAccess_token();
        hlsPath += "?token=" + token;
        return hlsPath;
    }
}
